//
//  ZTDateHandle.m
//  ZHYYTool
//
//  Created by MT-SW-005 on 2021/9/17.
//  Copyright © 2021 MT-SW-005. All rights reserved.
//

#import "ZTDateHandle.h"

@implementation ZTDateHandle

+ (NSString *)weiboTimeWithPublicTime:(NSTimeInterval)timeInterval timeStyel:(WeiBoTimeStyle)style
{
    NSTimeInterval interval;
    switch (style) {
        case WeiBoTimeStyleSecond:
                interval = [ZTDateHandle timeDifferenceOfSecondsWithStartTimeInterval:timeInterval endTimeInterval:[ZTDateHandle currentSecondTimeInterval]];
            break;
        case WeiBoTimeStyleMillisecond:
            interval = [ZTDateHandle timeDifferenceOfMillisecondWithStartTimeInterval:timeInterval endTimeInterval:[ZTDateHandle currentMilliSecondTimeInterval]];
            break;
        default:
            break;
    }
    
    if (interval < 10) {
        return @"刚刚";
    } else if (interval < 60) {
        return [NSString stringWithFormat:@"%.f秒前", interval];
    } else if (interval / 60 < 60) {
        return [NSString stringWithFormat:@"%.f分钟前", interval / 60];
    } else if (interval / 60 / 60 < 24) {
        return [NSString stringWithFormat:@"%.f小时前", interval / 60 / 60 - 1];
    } else if (interval / 60 / 60 / 24 < 30) {
        return [NSString stringWithFormat:@"%.f天前", interval / 60 / 60 / 24];
    } else if (interval / 60 / 60 / 24 / 30 < 12) {
        return [NSString stringWithFormat:@"%.f月前", interval / 60 / 60 / 24 / 30];
    } else if (interval / 60 / 60 / 24 / 30 / 12 >= 1){
        return [NSString stringWithFormat:@"%.f年前", interval / 60 / 60 / 24 / 30 / 12];
    } else {
        return @"未知时间";
    }
}


+(NSTimeInterval)currentMilliSecondTimeInterval
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle: NSDateFormatterMediumStyle];
    [formatter setTimeStyle: NSDateFormatterShortStyle];
    [formatter setDateFormat: @"YYYY-MM-dd HH:mm:ss SSS"];
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Shanghai"];
    [formatter setTimeZone: timeZone];
    NSDate *datenow = [NSDate date];
    return [datenow timeIntervalSince1970] * 1000;
}

+(NSTimeInterval)currentSecondTimeInterval
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle: NSDateFormatterMediumStyle];
    [formatter setTimeStyle: NSDateFormatterShortStyle];
    [formatter setDateFormat: @"YYYY-MM-dd HH:mm:ss SSS"];
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName: @"Asia/Shanghai"];
    [formatter setTimeZone: timeZone];
    NSDate *datenow = [NSDate date];
    return [datenow timeIntervalSince1970];
}

+(NSTimeInterval)timeDifferenceOfMillisecondWithStartTimeInterval:(NSTimeInterval)startTime endTimeInterval:(NSTimeInterval)endTime
{
    if (startTime < endTime) {
        return (endTime - startTime) / 1000;
    } else {
        return (startTime - endTime) / 1000;
    }
}


+ (NSTimeInterval)timeDifferenceOfSecondsWithStartTimeInterval:(NSTimeInterval)startTime endTimeInterval:(NSTimeInterval)endTime
{
    if (startTime < endTime) {
        return (endTime - startTime);
    } else {
        return (startTime - endTime);
    }
}




+(NSString*)formatTimeString:(NSString*)formatString
{
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatString];
    NSString *TimeString = [formatter stringFromDate:date];
    return TimeString;
}


+(NSString*)nowTimeString
{
    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];
    NSString *nowTime = [formatter stringFromDate:date];
    return nowTime;
}

+(NSString*)localTimeZone
{
    NSString *localTimeZone = [NSString stringWithFormat:@"%@",[NSTimeZone systemTimeZone]];
    NSLog(@"%@", localTimeZone);
    return localTimeZone;
}

@end
